<?php

require_once __DIR__ . '/../vendor/autoload.php';

/***************************/

class Wechat
{
    public $vbot;

//    private $_selfUserName;
//    protected $_bingUserName;
//    protected $_nowChaterUserName;
//    protected $_ChaterList = [];

    protected $message;

    function __construct()
    {
        date_default_timezone_set('Asia/Shanghai');//'Asia/Shanghai'   亚洲/上海
        $options = include __DIR__ . '/config.php';
        $vbot = new Hanson\Vbot\Foundation\Vbot($options);
        $this->vbot = $vbot;
        $observer = $vbot->observer;
        $observer->setFetchContactObserver([$this, 'initConfig']);
        try {
            $vbot->messageHandler->setHandler([$this, 'dealMsg']);//收到消息时触发
            $vbot->messageHandler->setCustomHandler([$this, 'dealCus']);//一直触发 为当 Vbot 向微信服务器请求查询是否有最新消息时，无论是否有新消息都会触发，触发周期最长为 35 秒一次。
        } catch (Exception $exception) {
            echo '第' . __LINE__ . '行报错了!';
        }

    }

    /**
     * 消息回调函数
     * @param $message
     */
    function dealMsg($message)
    {
        $this->message = $message;
        if ($this->isNotTouch()) {
            $this->echoMsg();
        }

        if ($this->isRecall()) {
            $this->recodeRecall();
            return;
        }
        if ($this->isMyCode()) {
            $this->dealMyCode();
            return;
        }

    }

    function dealCus(){

    }

    /***********非点击事件控制台输出****************/
    protected function isNotTouch()
    {
        if ($this->message['type'] != 'touch') {
            return true;
        } else {
            return false;
        }
    }

    protected function echoMsg()
    {
        echo $this->message['fromType'] . '\'s ' . $this->message['type'] . '
';
        $cont = [];
        $cont['from']['NickName'] = $this->message['from']['NickName'] ?? null;
        $cont['from']['RemarkName'] = $this->message['from']['RemarkName'] ?? null;
        $cont['from']['UserName'] = $this->message['from']['UserName'] ?? null;
        if ($this->message['sender'] != null) {
            $cont['sender']['NickName'] = $this->message['sender']['NickName'] ?? null;
            $cont['sender']['RemarkName'] = $this->message['sender']['RemarkName'] ?? null;
            $cont['sender']['UserName'] = $this->message['sender']['UserName'] ?? null;
            $cont['sender']['DisplayName'] = $this->message['sender']['DisplayName'] ?? null;
        }
        var_export($cont);
        echo '
        
';
    }

    /***********个人指令****************/
    protected function isMyCode()
    {
        if ($this->message['fromType'] == 'Self'
            and $this->message['from']['UserName'] == 'filehelper') {
            return true;
        } else {
            return false;
        }
    }

    protected function dealMyCode()
    {
        switch ($this->message['content']){
            case '在吗':\Hanson\Vbot\Message\Text::send('filehelper','在的');
        }
    }

    /************撤回消息***************/
    protected function isRecall()
    {
        if ($this->message['type'] == 'recall') {
            return true;
        } else {
            return false;
        }
    }

    protected function recodeRecall()
    {

//撤回消息记录
        Hanson\Vbot\Message\Text::send('filehelper', $this->message['nickname'] . ' 撤回了:' . $this->message['origin']['content']);

    }

    /*************初始化**************/
    function initConfig()
    {

    }
}
